﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Channels.Models;
using Twitterizer;
using Twitterizer.Entities;

namespace Channels.Controllers {
    public class ChannelController : Controller{

        [OutputCache(Duration = 20, VaryByParam = "None")]
        public ActionResult List() {
            List<Channel> list = new List<Channel>();
            if (!Request.IsAuthenticated) {
                list = ChannelsLogic.GetChannels();
            } else {
                list = ChannelsLogic.GetChannels(User.Identity.Name);
            }

            foreach (var item in list) {
                item.ChannelItems = ChannelsLogic.GetChannelItems(item.Id);
            }
            //list.Add(GetTweets());
            
            /*
            for (int j = 0; j < 5; j++) {
                Channel c = new Channel();
                c.Id = j;
                c.Title = string.Format("Channel - {0}", j.ToString());
                for (int i = 0; i < 20; i++) {
                    c.ChannelItems.Add(new ChannelItem() {
                        Title = "Hey This is testing This is testing This is testing" + j.ToString() + i.ToString(),
                        Desc = "This is testing"
                    });
                }
                list.Add(c);
            }
            
             * */

            return View(list);
        }

        public ActionResult Link(string url) {
            ViewBag.Url = url;
            return View();
        }

        private Channel GetTweets() {
            OAuthTokens t = new OAuthTokens();
            t.AccessToken = "9306942-nklZldNGRjIy17OMiylVZV6p2ExFJXlD33cZZrU";
            t.AccessTokenSecret = "M3CAJ1bK84Ah6IswyqxlsAqOlRbQBNgral9qfajo";
            t.ConsumerKey = "fZFbrmkWa5Pulg47OZ48Q";
            t.ConsumerSecret = "nKR1BtYKDHpQF08iGfZsAJ3CiJ3kTusO488HEMsGU";

            TimelineOptions to = new TimelineOptions();
            to.CacheOutput = true;
            to.Count = 50;
            to.IncludeRetweets = false;
            //to.MaxStatusId = 30;

            TwitterResponse<TwitterStatusCollection> timelineResult = TwitterTimeline.HomeTimeline(t, to);

            List<TwitterStatus> tweet = timelineResult.ResponseObject.Where(x => x.Entities != null && x.Entities.OfType<TwitterMentionEntity>().Count() > 0).ToList();

            Channel c = new Channel();

            foreach (var item in tweet)
	        {
                c.ChannelItems.Add(new ChannelItem() {
                    Desc = item.Text
                });
	        }

            ChannelItem itm = new ChannelItem();
            itm.Title = "Earthquake in Japan";
            c.ChannelItems.Add(itm);
            c.Title = "This is from Tweets";
            return c;
        }


        //public PartialViewResult GetListData() {
        //    List<Channel> list = new List<Channel>();

        //    for (int j = 0; j < 5; j++) {
        //        Channel c = new Channel();
        //        c.Id = j;
        //        c.Title = string.Format("Channel - {0}", j.ToString());
        //        for (int i = 0; i < 20; i++) {
        //            c.ChannelItems.Add(new ChannelItem() {
        //                Title = "Hey This is testing This is testing This is testing" + j.ToString() + i.ToString(),
        //                Desc = "This is testing"
        //            });
        //        }
        //        list.Add(c);
        //    }

        //    return PartialView(list);
        //}
        
    }
}